package cn.wolfcode.web.controller;

import cn.wolfcode.anno.RequirePermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequirePermission(name = "员工查询",expression = "employee:list")
    public String List(@ModelAttribute("qo") EmployeeQueryObject qo, Model model){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        return "employee/list";
    }

    @RequestMapping("/input")
    @RequirePermission(name = "员工新增和修改",expression = "employee:saveOrUpdate")
    public String input(Long id, Model model){
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles",roles);
        if (id != null){
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee",employee);
            List<Role> wonRoles = roleService.queryRoleLByEmployeeId(employee.getId());
            model.addAttribute("wonRoles",wonRoles);
        }
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments",departments);
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "员工新增和修改",expression = "employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds){
        JsonResult jsonResult = new JsonResult();
        try{
            if (employee.getId() == null){
                employeeService.insert(employee,roleIds);
            }else {
                employeeService.updateByPrimaryKey(employee,roleIds);
            }
            jsonResult.setSuccess(true);
            jsonResult.setMsg("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg("编辑失败");
        }
        return jsonResult;
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "员工删除",expression = "employee:delete")
    public String delete(Long id){
        employeeService.deleteByPrimaryKey(id);
        return "redirect:/employee/list";
    }

    @RequestMapping("/checkUserName")
    @ResponseBody
    public Map<String,Boolean> checkUsername(String username){
        boolean valid = employeeService.checkUsername(username);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",!valid);
        return map;
    }


}
